package unicall.em.models.vo;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;


import unicall.em.models.Skill;
import unicall.em.utils.StringUtil;

public class SkillSpec implements Specification<Skill> {

    private final Skill skill;
    
    public SkillSpec(Skill skill){
        this.skill = skill;
    }
    
    @Override
    public Predicate toPredicate(Root<Skill> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
        List<Predicate> predicates = new ArrayList<Predicate>();
        if (!StringUtil.isBlank(skill.getName())) {
            predicates.add(
                    cb.like(cb.lower(root.get(Media_.name)), skill.getName().toLowerCase() + "%"));
        }
        if (!StringUtil.isBlank(skill.getQueueId())) {
            predicates.add(cb.equal(root.get("queueId"), skill.getQueueId()));
        }
        return cb.and(predicates.toArray(new Predicate[0]));
    }
 
    
}
